﻿Imports System.Web.Security

Partial Class Profile
    Inherits BasePage

    Protected Sub Profile_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim usrName As String
            If Request.QueryString("user") Is Nothing AndAlso Page.RouteData.Values("user") Is Nothing Then
                If Page.User.Identity.IsAuthenticated Then
                    ' get logged in user
                    usrName = Page.User.Identity.Name
                Else
                    ' no user, not logged in....error
                    ErrorLog.LogErrors(New Exception("Unable to access user profile requested.(User:" & Page.User.Identity.Name & ",Profile:" & ")"))
                    Throw New Exception("Unable to access user profile requested.")
                End If
            ElseIf Not Request.QueryString("user") Is Nothing Then
                usrName = Request.QueryString("user")
            ElseIf Not Page.RouteData.Values("user") Is Nothing Then
                usrName = Page.RouteData.Values("user")
            Else
                ' no user, not logged in....error
                ErrorLog.LogErrors(New Exception("Unable to access user profile requested.(User:" & Page.User.Identity.Name & ",Profile:" & ")"))
                Throw New Exception("Unable to access user profile requested.")
            End If

            LoadUserData(usrName)

        End If

    End Sub
    Private Sub LoadUserData(ByVal usrName As String)
        Dim u As MembershipUser
        Dim p As ProfileCommon
        u = Membership.GetUser(usrName)
        p = Profile.GetProfile(usrName)
        txtEmail.Text = u.Email
        lblEmail.Text = u.Email
        txtFirstName.Text = p.FirstName
        lblFirstName.Text = p.FirstName
        txtLastName.Text = p.LastName
        lblLastName.Text = p.LastName
        lblUserName.Text = u.UserName
        lblUserSince.Text = u.CreationDate.ToShortDateString()
        SetEdit(u)

    End Sub
    Private Sub SetEdit(ByVal u As MembershipUser)
        If Page.User.IsInRole("Administrator") OrElse Page.User.Identity.Name = u.UserName Then
            lblEmail.Visible = False
            lblFirstName.Visible = False
            lblLastName.Visible = False
            txtEmail.Visible = True
            txtFirstName.Visible = True
            txtLastName.Visible = True
            btnSave.Visible = True
            If Page.User.Identity.Name.ToLower = u.UserName.ToLower Then
                pnlChangePass.Visible = True
            Else
                pnlChangePass.Visible = False
            End If
        Else
            lblEmail.Visible = True
            lblFirstName.Visible = True
            lblLastName.Visible = True
            txtEmail.Visible = False
            txtFirstName.Visible = False
            txtLastName.Visible = False
            btnSave.Visible = False
        End If
    End Sub
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim u As MembershipUser = Membership.GetUser(lblUserName.Text)
        If u.Email <> txtEmail.Text Then
            u.Email = txtEmail.Text
            Membership.UpdateUser(u)
        End If
        Dim p As ProfileCommon = Profile.GetProfile(u.UserName)
        p.FirstName = txtFirstName.Text
        p.LastName = txtLastName.Text
        p.Save()
    End Sub

    Protected Sub btnChangePass_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChangePass.Click
        If txtNewPass1.Text = txtNewPass2.Text Then
            Dim u As MembershipUser
            If Page.User.Identity.IsAuthenticated Then
                ' get logged in user
                u = Membership.GetUser(Page.User.Identity.Name)
            Else
                ' no user, not logged in....error
                Throw New Exception("Unable to access user profile requested.")
            End If
            If u.ChangePassword(txtOldPass.Text, txtNewPass1.Text) Then
                txtOldPass.Text = ""
                txtNewPass1.Text = ""
                txtNewPass2.Text = ""
                lblChangePass.Text = "Password Changed!"
            End If
        Else
            lblChangePass.Text = "New Passwords does not match!"
        End If
    End Sub
End Class
